3 * This is the MySQLi database abstraction layer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace Wikimedia\Rdbms
;
31 * Database abstraction object for PHP extension mysqli.
37 class DatabaseMysqli
extends DatabaseMysqlBase
{
40 * @return mysqli_result|bool
42 protected function doQuery( $sql ) {
43 $conn = $this->getBindingHandle();
45 if ( $this->bufferResults() ) {
46 $ret = $conn->query( $sql );
48 $ret = $conn->query( $sql, MYSQLI_USE_RESULT
);
55 * @param string $realServer
56 * @param string|null $dbName
58 * @throws DBConnectionError
60 protected function mysqlConnect( $realServer, $dbName ) {
61 # Avoid suppressed fatal error, which is very hard to track down
62 if ( !function_exists( 'mysqli_init' ) ) {
63 throw new DBConnectionError( $this, "MySQLi functions missing,"
64 . " have you compiled PHP with the --with-mysqli option?\n" );
67 // Other than mysql_connect, mysqli_real_connect expects an explicit port
68 // and socket parameters. So we need to parse the port and socket out of
72 $hostAndPort = IP
::splitHostAndPort( $realServer );
74 $realServer = $hostAndPort[0];
75 if ( $hostAndPort[1] ) {
76 $port = $hostAndPort[1];
78 } elseif ( substr_count( $realServer, ':' ) == 1 ) {
79 // If we have a colon and something that's not a port number
80 // inside the hostname, assume it's the socket location
81 $hostAndSocket = explode( ':', $realServer, 2 );
82 $realServer = $hostAndSocket[0];
83 $socket = $hostAndSocket[1];
86 $mysqli = mysqli_init();
89 if ( $this->flags
& self
::DBO_SSL
) {
90 $connFlags |
= MYSQLI_CLIENT_SSL
;
99 if ( $this->flags
& self
::DBO_COMPRESS
) {
100 $connFlags |
= MYSQLI_CLIENT_COMPRESS
;
102 if ( $this->flags
& self
::DBO_PERSISTENT
) {
103 $realServer = 'p:' . $realServer;
106 if ( $this->utf8Mode
) {
107 // Tell the server we're communicating with it in UTF-8.
108 // This may engage various charset conversions.
109 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'utf8' );
111 $mysqli->options( MYSQLI_SET_CHARSET_NAME
, 'binary' );
113 $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT
, 3 );
115 if ( $mysqli->real_connect(
130 protected function connectInitCharset() {
131 // already done in mysqlConnect()
136 * @param string $charset
139 protected function mysqlSetCharset( $charset ) {
140 $conn = $this->getBindingHandle();
142 return $conn->set_charset( $charset );
148 protected function closeConnection() {
149 $conn = $this->getBindingHandle();
151 return $conn->close();
157 function insertId() {
158 $conn = $this->getBindingHandle();
160 return (int)$conn->insert_id
;
166 function lastErrno() {
167 if ( $this->conn
instanceof mysqli
) {
168 return $this->conn
->errno
;
170 return mysqli_connect_errno();
177 protected function fetchAffectedRowCount() {
178 $conn = $this->getBindingHandle();
180 return $conn->affected_rows
;
183 function doSelectDomain( DatabaseDomain
$domain ) {
184 if ( $domain->getSchema() !== null ) {
185 throw new DBExpectedError( $this, __CLASS__
. ": domain schemas are not supported." );
188 $database = $domain->getDatabase();
189 if ( $database !== $this->getDBname() ) {
190 $conn = $this->getBindingHandle();
191 if ( !$conn->select_db( $database ) ) {
192 throw new DBExpectedError( $this, "Could not select database '$database'." );
196 // Update that domain fields on success (no exception thrown)
197 $this->currentDomain
= $domain;
203 * @param mysqli_result $res
206 protected function mysqlFreeResult( $res ) {
213 * @param mysqli_result $res
214 * @return stdClass|bool
216 protected function mysqlFetchObject( $res ) {
217 $object = $res->fetch_object();
218 if ( $object === null ) {
226 * @param mysqli_result $res
229 protected function mysqlFetchArray( $res ) {
230 $array = $res->fetch_array();
231 if ( $array === null ) {
239 * @param mysqli_result $res
242 protected function mysqlNumRows( $res ) {
243 return $res->num_rows
;
247 * @param mysqli_result $res
250 protected function mysqlNumFields( $res ) {
251 return $res->field_count
;
255 * @param mysqli_result $res
259 protected function mysqlFetchField( $res, $n ) {
260 $field = $res->fetch_field_direct( $n );
262 // Add missing properties to result (using flags property)
263 // which will be part of function mysql-fetch-field for backward compatibility
264 $field->not_null
= $field->flags
& MYSQLI_NOT_NULL_FLAG
;
265 $field->primary_key
= $field->flags
& MYSQLI_PRI_KEY_FLAG
;
266 $field->unique_key
= $field->flags
& MYSQLI_UNIQUE_KEY_FLAG
;
267 $field->multiple_key
= $field->flags
& MYSQLI_MULTIPLE_KEY_FLAG
;
268 $field->binary
= $field->flags
& MYSQLI_BINARY_FLAG
;
269 $field->numeric = $field->flags
& MYSQLI_NUM_FLAG
;
270 $field->blob
= $field->flags
& MYSQLI_BLOB_FLAG
;
271 $field->unsigned
= $field->flags
& MYSQLI_UNSIGNED_FLAG
;
272 $field->zerofill
= $field->flags
& MYSQLI_ZEROFILL_FLAG
;
278 * @param mysqli_result $res
282 protected function mysqlFieldName( $res, $n ) {
283 $field = $res->fetch_field_direct( $n );
289 * @param mysqli_result $res
293 protected function mysqlFieldType( $res, $n ) {
294 $field = $res->fetch_field_direct( $n );
300 * @param mysqli_result $res
304 protected function mysqlDataSeek( $res, $row ) {
305 return $res->data_seek( $row );
309 * @param mysqli|null $conn Optional connection object
312 protected function mysqlError( $conn = null ) {
313 if ( $conn === null ) {
314 return mysqli_connect_error();
321 * Escapes special characters in a string for use in an SQL statement
325 protected function mysqlRealEscapeString( $s ) {
326 $conn = $this->getBindingHandle();
328 return $conn->real_escape_string( (string)$s );
332 * Give an id for the connection
334 * mysql driver used resource id, but mysqli objects cannot be cast to string.
337 public function __toString() {
338 if ( $this->conn
instanceof mysqli
) {
339 return (string)$this->conn
->thread_id
;
341 // mConn might be false or something.
342 return (string)$this->conn
;
349 protected function getBindingHandle() {
350 return parent
::getBindingHandle();
355 * @deprecated since 1.29
357 class_alias( DatabaseMysqli
::class, 'DatabaseMysqli' );